package com.xt.controller;

import com.xt.bean.Course;
import com.xt.bean.PlatformResult;
import com.xt.service.CourseService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * Created by XT on 2018/5/4.
 */
@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    CourseService cs;

    @ApiOperation(value = "新增/修改信息", notes = "新增/修改信息")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public PlatformResult insert(@ApiParam(required = true, name = "course", value = "Course实体的字段名") @RequestBody Course c) {
        //检查参数
        cs.saveOrUpdateOne(c);
        return PlatformResult.ok();
    }

    @ApiOperation(value = "删除信息", notes = "删除信息")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public PlatformResult delete(@ApiParam(required = true, name = "course", value = "Course实体的字段名") @RequestBody Course c) {
        //检查参数
        cs.deleteOne(c.getId());
        return PlatformResult.ok();
    }

    @ApiOperation(value = "根据id查", notes = "根据id查")
    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    public PlatformResult getById(@ApiParam(required = true, name = "id", value = "id") @RequestParam String id) {
        Course c = cs.getById(id);
        return PlatformResult.ok(c);
    }

    @ApiOperation(value = "列表", notes = "查询列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public PlatformResult list(
            @ApiParam(required = true, name = "departmentId", value = "院系ID") @RequestParam String departmentId,
            @ApiParam(required = true, name = "specialtyId", value = "专业ID") @RequestParam String specialtyId,
            @ApiParam(required = true, name = "name", value = "课程名称") @RequestParam String name,
            @ApiParam(required = true, name = "count", value = "每页条数") @RequestParam int count,
            @ApiParam(required = true, name = "pageNo", value = "当前页") @RequestParam int pageNo,
            @ApiParam(required = true, name = "sortDirection", value = "排序方式（asc/desc）") @RequestParam String sortDirection,
            @ApiParam(required = true, name = "sortField", value = "排序字段") @RequestParam String[] sortField) {
        Page<Course> page = cs.getListByPage(departmentId, specialtyId, name, count, pageNo, sortDirection, sortField);
        return PlatformResult.ok(page);
    }

}
